package com.ly.hotel.spainterface.jg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.spainterface.jg.bean.MassageItem;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ThinkPad on 2018/2/27.
 */

public class MassageOperateInfoAdapter extends BaseAdapter {

    private Context context;
    private List<MassageItem> massageItemList;
    private Map<String,Boolean> map = new HashMap<>();

    public Map<String, Boolean> getMap() {
        return map;
    }

    public MassageOperateInfoAdapter(Context context, List<MassageItem> massageItemList){
        this.context = context;
        this.massageItemList = massageItemList;
    }

    @Override
    public int getCount() {
        return massageItemList.size();
    }

    @Override
    public Object getItem(int position) {
        return massageItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder viewHolder;
        if(convertView == null){
            view = LayoutInflater.from(context).inflate(R.layout.jg_listview_massage_info_hide_item,null);
            viewHolder = new ViewHolder();
            viewHolder.goodsHideNo = (TextView) view.findViewById(R.id.goods_hide_no);
            viewHolder.goodsHideName = (TextView) view.findViewById(R.id.goods_hide_name);
            viewHolder.goodsHideOperate = (CheckBox) view.findViewById(R.id.goods_hide_operate);
            view.setTag(viewHolder);
        }else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        MassageItem massageItem = massageItemList.get(position);
        final String massageItemId = massageItem.getId();
        String massageItemName = massageItem.getName();
        // 记录CheckBox的状态
        viewHolder.goodsHideOperate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    map.put(massageItemId,true);
                }else{
                    map.remove(massageItemId);
                }
            }
        });

        // 根据状态还原CheckBox的状态
        if(map != null && map.containsKey(massageItemId)){
            viewHolder.goodsHideOperate.setChecked(true);
        }else{
            viewHolder.goodsHideOperate.setChecked(false);
        }

        viewHolder.goodsHideNo.setText(massageItemId);
        viewHolder.goodsHideName.setText(massageItemName);
        return view;
    }

    class ViewHolder{
        TextView goodsHideNo;
        TextView goodsHideName;
        CheckBox goodsHideOperate;
    }
}
